package com.xuzhijie.pms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.intern.InternUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuzhijie.pms.common.Constants;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.Feedback;
import com.xuzhijie.pms.entity.Notice;
import com.xuzhijie.pms.entity.vo.NoticeVo;
import com.xuzhijie.pms.exception.PmsServerException;
import com.xuzhijie.pms.mapper.FeedbackMapper;
import com.xuzhijie.pms.service.IFeedbackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuzhijie.pms.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-17
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements IFeedbackService {

    @Override
    public Result findPage(Integer page,
                           Integer size,
                           Integer progress,
                           Integer feedbackType,
                           Integer feedbackClassification,
                           Integer source) {

        System.out.println("progress:"+progress);
        System.out.println("feedbackType:"+feedbackType);
        System.out.println("feedbackClassification:"+feedbackClassification);
        System.out.println("source:"+source);

        Page<Feedback> feedbackPage = new Page<>(page,size);
        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<>();
        Page<Feedback> result;
        Long total;

        if (!progress.equals(0)){
            queryWrapper.eq("progress",progress);
        }

        if (!feedbackType.equals(0)){
            queryWrapper.eq("feedback_type",feedbackType);
        }

        if (!feedbackClassification.equals(0)){
            queryWrapper.eq("feedback_classification",feedbackClassification);
        }

        if (!source.equals(0)){
            queryWrapper.eq("source",source);
        }

        try {
            result = page(feedbackPage,queryWrapper);
            total = count(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            throw new PmsServerException("业务繁忙", Constants.SYSTEM_ERROR);
        }

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("records",result.getRecords());
        resultMap.put("total",total);

        return Result.success(resultMap);

    }

    @Override
    public Result addSuggestion(Map<String, Object> request) {

        Object feedbackClassification = request.get("feedbackClassification");
        Object feedbackType = request.get("feedbackType");
        Object description = request.get("description");
        Object images = request.get("images");

        Feedback feedback = new Feedback();

        if (!images.toString().equals("[]")){
            feedback.setImages(request.get("images").toString());
        }

        if (!feedbackClassification.equals("")){
            feedback.setFeedbackClassification(Integer.parseInt(feedbackClassification.toString()));
        }

        feedback.setFeedbackType(Integer.parseInt(feedbackType.toString()));

        if (!description.equals("")){
            feedback.setDescription(description.toString());
        }

        feedback.setSource(1);
        feedback.setProgress(1);
        save(feedback);

        return Result.success();
    }
}
